package me.keiwu.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import me.keiwu.admin.rpc.MedalRpc;
import me.keiwu.pub.data.ResponseV2;
import me.keiwu.pub.data.service.MedalPresentBean;
import me.keiwu.pub.util.excel.ExcelUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by kei on 30/01/2018.
 */
@RestController
@RequestMapping("/api/medal")
public class MedalController {

    private static final Logger logger = LoggerFactory.getLogger(MedalController.class);


    @Autowired
    private MedalRpc medalRpc;
    @Autowired
    private ExcelUtil excelUtil;


    @RequestMapping("list")
    public ResponseV2 list(@RequestParam(defaultValue = "0") Integer page,
                           @RequestParam(defaultValue = "10") Integer size) {
        return ResponseV2.ok(JSON.parse(medalRpc.list(page, size)));
    }

    @RequestMapping("save")
    public ResponseV2 save(@RequestParam(required = false) Integer medalId,
                           String name, String url, Integer cost) {
        String res = medalRpc.save(medalId, name, url, cost);
        if (StringUtils.isBlank(res)) {
            return ResponseV2.ok("保存勋章失败");
        }
        return ResponseV2.ok(JSON.parse(res));

    }


    @RequestMapping("shelve")
    public ResponseV2 shelve(Integer medalId, boolean shelve) {
        String res = medalRpc.shelve(medalId, shelve);
        if (StringUtils.isBlank(res)) return ResponseV2.fail("没有该勋章");
        return ResponseV2.ok(JSON.parse(res));

    }

    @RequestMapping("present/send")
    public ResponseV2 sendMedalPresent(Integer presentId) {
        String res = medalRpc.sendMedalPresent(presentId);
        if (StringUtils.isBlank(res)) return ResponseV2.fail("没有该礼物");
        return ResponseV2.ok(JSON.parse(res));

    }


    @RequestMapping("present/list")
    public ResponseV2 listPresent(@RequestParam(defaultValue = "0") Integer page,
                                  @RequestParam(defaultValue = "10") Integer size) {
        String res = medalRpc.listPresent(page, size);
        return ResponseV2.ok(JSON.parse(res));
    }

    @RequestMapping("present/detail")
    public ResponseV2 presentDetail(Integer presentId) {
        String res = medalRpc.presentDetail(presentId);
        return ResponseV2.ok(JSON.parse(res));
    }


    @RequestMapping("present/export")
    public ResponseV2 exportPresent(HttpServletResponse response) throws IOException {
        String res = medalRpc.findPresent();
        if (StringUtils.isBlank(res)) return ResponseV2.ok();
        List<MedalPresentBean> list = JSON.parseArray(res, MedalPresentBean.class);
        logger.info("exportPresent data size={}", list.size());
        excelUtil.write(response, excelUtil.simpleExport(list), "export.xls");
        return ResponseV2.ok();
    }

}
